package org.timeismoney.payment.web.action.teacher;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.exception.BizException;
import org.timeismoney.payment.model.SchoolYear;
import org.timeismoney.payment.model.Student;
import org.timeismoney.payment.model.Unit;
import org.timeismoney.payment.service.SchoolYearService;
import org.timeismoney.payment.service.StudentService;
import org.timeismoney.payment.service.UnitService;
import org.timeismoney.payment.web.ExcelUtil;
import org.timeismoney.payment.web.action.BaseAction;

public class DownloadAction extends BaseAction {
	private static Logger	log	= Logger.getLogger(DownloadAction.class);
	private SchoolYearService schoolYearService;
	private StudentService studentService;
	private UnitService unitService;

	public void setUnitService(UnitService unitService) {
		this.unitService = unitService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public void setSchoolYearService(SchoolYearService schoolYearService) {
		this.schoolYearService = schoolYearService;
	}

	public ActionForward template(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setHeader("Cache-Control", "store");
		response.setHeader("Pragma", "cache");
		response.setDateHeader("Expires", -1);
		String filename = "导入学生模板.xls";
		try {
			ServletOutputStream out = response.getOutputStream();
			HSSFWorkbook workbook;
			if (request.getParameter("all")==null) {
				workbook = ExcelUtil.generateTemplate();
			}else{
				workbook = ExcelUtil.generateTemplateWithUnitNo();
			}
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(filename,"UTF-8"));
			workbook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			renderText(response, "下载模板出错：" + e.getMessage());
		}

		return null;
	}
	
	//add by chamdenjo@gmail.com
	public ActionForward unitTemplate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setHeader("Cache-Control", "store");
		response.setHeader("Pragma", "cache");
		response.setDateHeader("Expires", -1);
		String filename = "院系模板.xls";
		try {
			ServletOutputStream out = response.getOutputStream();
			HSSFWorkbook workbook = ExcelUtil.generateUnitTemplate();
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(filename,"UTF-8"));
			workbook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			renderText(response, "下载模板出错：" + e.getMessage());
		}

		return null;
	}
	
	//add by chamdenjo@gmail.com
	public ActionForward downloadAllUnit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setHeader("Cache-Control", "store");
		response.setHeader("Pragma", "cache");
		response.setDateHeader("Expires", -1);
		String filename = "院系结构数据.xls";
		try {
			ServletOutputStream out = response.getOutputStream();
			List<Unit> leafUnits = unitService.findAllLeafUnit(4);
			for (Unit unit:leafUnits){
				Unit parentUnit = unit.getParentUnit();
				while (parentUnit != null){
					parentUnit = parentUnit.getParentUnit();
				}
			}
			HSSFWorkbook workbook = ExcelUtil.generateAllUnit(leafUnits);
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(filename,"UTF-8"));
			workbook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			renderText(response, "下载模板出错：" + e.getMessage());
		}

		return null;
	}


	public ActionForward report(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setHeader("Cache-Control", "store");
		response.setHeader("Pragma", "cache");
		response.setDateHeader("Expires", -1);
		try {
			String yearNoStr = request.getParameter("schoolYearNo");
			int yearNo = Integer.valueOf(yearNoStr);
			SchoolYear schoolYear = schoolYearService.get(yearNo);
			if(schoolYear==null){
				throw new BizException("该学年不存在！");
			}
			String filename = schoolYear.getSchoolYearName()+"--学生报表.xls";
			ServletOutputStream out = response.getOutputStream();
			List report = studentService.getStuPaymentReport(schoolYear);
			HSSFWorkbook workbook = ExcelUtil.generateReport(report,schoolYear);
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(filename,"UTF-8"));
			workbook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			renderText(response, "生成报表出错：" + e.getMessage());
		}

		return null;
	}
	
	public ActionForward downloadStudents(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.setHeader("Cache-Control", "store");
		response.setHeader("Pragma", "cache");
		response.setDateHeader("Expires", -1);
		try {
			ServletOutputStream out = response.getOutputStream();
			String unitNoStr = request.getParameter("unitNo");
			int unitNo = Integer.valueOf(unitNoStr);
			Unit unit = unitService.get(unitNo);
			if(unit==null){
				throw new BizException("该班级不存在！");
			}
			String filename = unit.getUnitName()+"学生报表.xls";
			List<Student> stus = studentService.findStuByUnit(unitNo);
			HSSFWorkbook workbook = ExcelUtil.generateStudentsReport(stus, unit);
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");
			response.setHeader("Content-disposition", "attachment; filename="
					+ URLEncoder.encode(filename,"UTF-8"));
			workbook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			renderText(response, "生成报表出错：" + e.getMessage());
		}

		return null;
	}
}
